package com.hboxs.asl.controller.app.index;

import com.hboxs.asl.Filter;
import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.Page;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.controller.app.BaseController;
import com.hboxs.asl.entity.Merchant;
import com.hboxs.asl.entity.Product;
import com.hboxs.asl.service.MerchantService;
import com.hboxs.asl.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by hboxs on 2016/12/22
 * 商家店铺
 */
@Controller("appMerchantController")
@RequestMapping(value = "/app/merchant")
public class MerchantController extends BaseController {

    @Resource(name = "merchantServiceImpl")
    private MerchantService merchantService;
    @Resource(name = "productServiceImpl")
    private ProductService productService;

    /**
     * 店铺详情
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public
    @ResponseBody
    Object index(Product.OrderType orderType,  Long mId , Pageable pageable ) {

        Merchant merchant = merchantService.find(mId);

        if(merchant==null){
            return JsonMessage.error("暂无商家信息");
        }else{
            pageable.getFilters().add(Filter.eq("merchant" , merchant));
        }


        Page page = productService.findPageGroup(
                null, null, null, null, null, null, null, true, null, null, null, null, orderType, pageable
        );

        return JsonMessage.success(

                "totalPage", page.getTotalPages(),

                "pageNumber", page.getPageNumber(),

                "pageSize", page.getPageSize(),

                "total", page.getTotal(),

                "content", JsonMessage.optimiseObj(
                        page.getContent(),
                        "id", "image", "name", "price", "promotionPrice", "finalScore", "weight", "sales" ,"fakeSale"
                ),

                "merchant" , JsonMessage.optimiseObj(merchantService.find(mId),"id","name","logo","banner")

        );



    }



}
